package com.qdu.seq.controller;

import com.qdu.seq.util.IEquationPrinter;
import com.qdu.seq.util.IPaper;
import com.qdu.seq.util.SimpleEquationPrinter;
import com.qdu.seq.util.TestPaper;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@EnableAutoConfiguration
public class index {

    @RequestMapping({"/"})
    public String index() {
        return "index";
    }

    @RequestMapping(value = {"/proceed"}, method = {RequestMethod.POST})
    public String execute(String number, int operator, boolean showanswer, Model model) {
        if (number.equals("")) {
            model.addAttribute("msg", "请输入要生成的计算题数目再提交");
            return "index";
        } else {
            int bound = 100;
            int questionsEachLine = 5;
            IPaper testPaper = new TestPaper(Integer.parseInt(number), bound, operator);
            IEquationPrinter equationPrinter = new SimpleEquationPrinter();
            model.addAttribute("paper", equationPrinter.printPaper(testPaper, questionsEachLine, showanswer));
            if (!showanswer) {
                model.addAttribute("paper2", equationPrinter.printPaper(testPaper, questionsEachLine, true));
            }
            return "result";
        }
    }
}
